package com.wu.findnumberswithsum;

import java.util.ArrayList;

/**
 * 和为S的两个数字
 *
 * @author lynn
 * @date 2020/8/13 - 15:41
 */
public class Solution {
    public static ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
        // 1,2,3,4,5,6 =》 7
        ArrayList<Integer> list = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        if (array == null || array.length == 0 || sum < array[0]) {
            return list;
        }
        int i = 0;
        int j = array.length;
        while (i < j){
            int num = array[i] + array[j - 1];
            if (num == sum){
                if (array[i] * array[j - 1] < min){
                    min = array[i] * array[j - 1];
                    if (list != null){
                        list.clear();
                    }
                    list.add(array[i]);
                    list.add(array[j - 1]);
                }
                i++;
                j--;
            }else if (num < sum){
                i++;
            }else {
                j--;
            }
        }
        return list;
    }
}
